package com.zyz.H_比较器.Comparator比较器;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author: vick
 * @Date: 2025/3/19
 * @Description:
 * @version: 1.0
 */
public class Test2 {
    public static void main(String[] args) {
        Shop[] shops = {
                new Shop("vick-A", 34),
                new Shop("vick-B", 44),
                new Shop("vick-E", 112),
                new Shop("vick-A", 54),
                new Shop("vick-D", 64),
                new Shop("vick-D", 74)
        };

        Arrays.sort(shops, new Comparator<Shop>() {
            @Override
            public int compare(Shop o1, Shop o2) {
                //return (int) (o1.getPrice() - o2.getPrice());
                //如果名字相同，按照价格降序
                //否则按照名字升序
                if (o1.getName().equals(o2.getName())) {
                    return -Double.compare(o1.getPrice(),o2.getPrice());
                }else {
                    return o1.getName().compareTo(o2.getName());
                }
            }
        });

        System.out.println(Arrays.toString(shops));

    }
}
